package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.UserCart;
import org.csu.mypetstore.service.UserCartService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "ViewCartServlet", value = "/ViewCartServlet")
public class ViewCartServlet extends HttpServlet {
    private static final String VIEW_CART="/WEB-INF/jsp/cart/ViewCart.jsp";
    //    private Cart cart;
//    private Iterator<CartItem> cartItemIterator;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        HttpSession session=request.getSession();
//        Cart cart =(Cart)session.getAttribute("cart");
//
//        if(cart == null){
//            cart= new Cart();
//            session.setAttribute("cart",cart);
//        }
//        request.getRequestDispatcher(VIEW_CART).forward(request,response);

//        HttpSession session=request.getSession();
//        String username=((Account)session.getAttribute("accounts")).getUsername();
//        cart = (Cart) session.getAttribute("cart");
//        cartItemIterator = cart.getAllCartItems();
//        CartItem cartItem = cartItemIterator.next();
//        String itemId = cartItem.getItem().getItemId();

        HttpSession session=request.getSession();
        String username=((Account)session.getAttribute("accounts")).getUsername();
        List<UserCart> userCartList=new ArrayList<UserCart>();
        UserCartService service=new UserCartService();
        userCartList=service.getCartListByUserid(username);
        session.setAttribute("userCartList",userCartList);
        request.getRequestDispatcher(VIEW_CART).forward(request,response);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}
